#!/bin/bash

REALM_CMAKE_CONFIGURATION=${REALM_CMAKE_CONFIGURATION:-Debug}

for i in "$@"
do
case $i in
  -c=*|--configuration=*)
    REALM_CMAKE_CONFIGURATION="${i#*=}"
    shift
  ;;
  *)
    EXTRA_CMAKE_ARGS="$EXTRA_CMAKE_ARGS $i"
  ;;
esac
done

SCRIPT_DIRECTORY="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

REALM_CMAKE_SUBPLATFORM=${REALM_CMAKE_SUBPLATFORM:-$(uname -s)}
REALM_CMAKE_INSTALL_TARGET=${REALM_CMAKE_INSTALL_TARGET:-install}
CMAKE_BINARY_DIR="$SCRIPT_DIRECTORY/cmake/$REALM_CMAKE_SUBPLATFORM"
CMAKE_INSTALL_PREFIX="$SCRIPT_DIRECTORY/build"

mkdir -p "$CMAKE_BINARY_DIR"
mkdir -p "$CMAKE_INSTALL_PREFIX"

cd $CMAKE_BINARY_DIR
cmake "$SCRIPT_DIRECTORY" -DCMAKE_INSTALL_PREFIX="$CMAKE_INSTALL_PREFIX" -DCMAKE_BUILD_TYPE=$REALM_CMAKE_CONFIGURATION $EXTRA_CMAKE_ARGS
cmake --build . --target $REALM_CMAKE_INSTALL_TARGET --config $REALM_CMAKE_CONFIGURATION
